﻿using System.Drawing;
using System.Drawing.Drawing2D;
using System.IO;
using System.Web;
using System.Drawing.Imaging;
using System;

namespace BenzInfo.Hockey.Grenadiers.Utils
{
    /// <summary>
    /// Description résumée de Image
    /// </summary>
    public class ImageHandler : IHttpHandler
    {
        public void ProcessRequest(HttpContext context)
        {            
            string src = context.Request["src"];
            
            int width = int.TryParse(context.Request["width"], out width) ? width : 0;
            int height = int.TryParse(context.Request["height"], out height) ? height : 0;

            int maxwidth = int.TryParse(context.Request["maxwidth"], out maxwidth) ? maxwidth : 0;
            int maxheight = int.TryParse(context.Request["maxheight"], out maxheight) ? maxheight : 0;

            try
            {
                using (Bitmap img = new Bitmap(context.Server.MapPath("~/Content/images/" + src)))
                {
                    if (maxwidth > 0 && maxheight > 0)
                    {
                        float wratio = maxwidth / (float)img.Width;
                        float hratio = maxheight / (float)img.Height;

                        if (wratio < hratio)
                        {
                            width = Math.Min(img.Width, maxwidth);
                            height = (int)(img.Height / (float)img.Width * width);
                        }
                        else
                        {
                            height = Math.Min(img.Height, maxheight);
                            width = (int)(img.Width / (float)img.Height * height);
                        }
                    }
                    else
                    {
                        if (width > 0 && height == 0)
                        {
                            width = width > img.Width ? img.Width : width;
                            height = (int)(img.Height / (float)img.Width * width);
                        }
                        else if (height > 0 && width == 0)
                        {
                            height = height > img.Height ? img.Height : height;
                            width = (int)(img.Width / (float)img.Height * height);
                        }
                        else
                        {
                            width = img.Width;
                            height = img.Height;
                        }
                    }

                    /*Bitmap newimg = new Bitmap(width, height);
                    Graphics g = Graphics.FromImage(newimg);
                    g.InterpolationMode = InterpolationMode.HighQualityBicubic;
                    g.SmoothingMode = SmoothingMode.HighQuality;
                    g.PixelOffsetMode = PixelOffsetMode.HighQuality;
                    g.CompositingQuality = CompositingQuality.HighQuality;
                    g.DrawImage(img, 0, 0, width, height);
                    */

                    Bitmap newimg = new Bitmap(img, width, height);

                    string ext = src.Split('.')[1];

                    switch (ext)
                    {
                        case "png":
                            context.Response.ContentType = "image/png";
                            newimg.Save(context.Response.OutputStream, ImageFormat.Png);
                            break;
                        case "gif":
                            context.Response.ContentType = "image/gif";
                            newimg.Save(context.Response.OutputStream, ImageFormat.Gif);
                            break;
                        default:
                            context.Response.ContentType = "image/jpeg";
                            newimg.Save(context.Response.OutputStream, ImageFormat.Jpeg);
                            break;
                    }
                }
            }
            catch (Exception)
            {
                width = width > 0 ? width : maxwidth > 0 ? maxwidth : 1;
                height = height > 0 ? height : maxheight > 0 ? maxheight : 1;

                using (Bitmap newimg = new Bitmap(width, height))
                {
                    Graphics g = Graphics.FromImage(newimg);
                    g.InterpolationMode = InterpolationMode.HighQualityBicubic;
                    g.SmoothingMode = SmoothingMode.HighQuality;
                    g.PixelOffsetMode = PixelOffsetMode.HighQuality;
                    g.CompositingQuality = CompositingQuality.HighQuality;

                    g.FillRectangle(Brushes.White, 0, 0, width - 1, height - 1);

                    /*using (Bitmap logo = new Bitmap(context.Server.MapPath("~/Content/images/bg_names_resize.png")))
                    {
                        g.DrawImage(logo, 0, 0, width, height);
                    }*/

                    g.DrawRectangle(Pens.Red, 0, 0, width - 1, height - 1);
                    g.DrawLine(Pens.Red, width - 1, 0, 0, height - 1);
                    g.DrawLine(Pens.Red, 0, 0, width - 1, height - 1);

                    using (Bitmap logo = new Bitmap(context.Server.MapPath("~/Content/images/logos/img_team_grenadiers.png")))
                    {
                        g.DrawImage(logo, (width-logo.Width)/2.0f,(height-logo.Height)/2.0f);
                    }
                    //g.DrawString("Error: Image not found!", new Font(FontFamily.GenericSansSerif, 10.0f), Brushes.Black, 0, 0);
                    //g.DrawString(src, new Font(FontFamily.GenericSansSerif, 10.0f), Brushes.Black, 0, 20);

                    context.Response.ContentType = "image/png";
                    //newimg.Save(context.Response.OutputStream, ImageFormat.Png);
                }
            }
        }

        public bool IsReusable
        {
            get
            {
                return false;
            }
        }
    }
}